/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.util;

import com.google.inject.Singleton;
import exatrez.stripes.BaseActionBean;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import net.sourceforge.stripes.util.Base64;

/**
 *
 * @author wan.fairul
 */
@Singleton
public class EncyptUtil extends BaseActionBean{
    

    
   public synchronized String encrypt(String plainText) {
        MessageDigest md;

        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            String msg = "SHA not found, encryption cannot continue, no recovery possible";
            throw new RuntimeException(msg, e);
        }

        try {
            md.update(plainText.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            String msg = "UTF-8 encoding not found, no recovery possible";
            throw new RuntimeException(msg, e);
        }

        byte[] raw = md.digest();

        //String pwd = new BASE64Encoder().encode(raw);
        String pwd = Base64.encodeBytes(raw);
        return pwd.length() > 50 ? pwd.substring(0, 50) : pwd;
    }
   
   public static void main (String ... args) {
       System.out.println("" + new EncyptUtil().encrypt("ivSlD8YaSw"));
   }
    
}
